﻿#include "camera_qdialog.h"
#include "qpluginloader.h"
#include "camera_dalsa.h"
#include <QDesktopWidget>
#include <QHeaderView>



#include "para.h"

CameraBaseQDialog::CameraBaseQDialog(QDialog *parent, QString dir,int camera_index) : QDialog(parent)
{
    m_camera_index = camera_index;
    QString path = dir + "/" + "camera_para/camera_" +  my_para.m_vector_camera_name[m_camera_index] + ".ini";
	m_qsettings = new QSettings(path, QSettings::IniFormat);

	m_section = "camera" + QString::number(camera_index);


    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width() / 1920.0;
    double ratio_y = desktop_rect.height() / 1080.0;

    move(0, 0);
    setFixedSize(700 * ratio_x, 1010 * ratio_y);

	QWidget* p0 = new QWidget(this);
	p0->move(0 * ratio_x, 0 * ratio_y);
	p0->setFixedSize(600 * ratio_x, 1010 * ratio_y);

	QGridLayout* qgridLayout = new QGridLayout();
	p0->setLayout(qgridLayout);
	qgridLayout->setVerticalSpacing(5);
	qgridLayout->setContentsMargins(0, 0, 0, 0);

    QSize control_size(220,40);

    int index = 0;

	QLabel* p = new QLabel();
	p->setText("Exposure Time(in us)");
	p->setFixedSize(control_size);
	qgridLayout->addWidget(p, index, 0);
	m_doubleqspinbox_ExposureTime = new QDoubleSpinBox();
	m_doubleqspinbox_ExposureTime->setFixedSize(control_size);
	m_doubleqspinbox_ExposureTime->setMinimum(0);
	m_doubleqspinbox_ExposureTime->setMaximum(100000);
	qgridLayout->addWidget(m_doubleqspinbox_ExposureTime, index, 1);
	index++;

	QLabel* p1 = new QLabel();
	p1->setText("Gain");
	p1->setFixedSize(control_size);
	qgridLayout->addWidget(p1, index, 0);
	m_doubleqspinbox_Gain = new QDoubleSpinBox();
	m_doubleqspinbox_Gain->setFixedSize(control_size);
	m_doubleqspinbox_Gain->setMinimum(0);
	m_doubleqspinbox_Gain->setMaximum(100000);
	qgridLayout->addWidget(m_doubleqspinbox_Gain, index, 1);
	index++;

	p = new QLabel();
	p->setText("Width");
	p->setFixedSize(control_size);
	qgridLayout->addWidget(p, index, 0);
	m_qspinbox_Width = new QSpinBox();
	m_qspinbox_Width->setFixedSize(control_size);
	m_qspinbox_Width->setMinimum(0);
	m_qspinbox_Width->setMaximum(100000);
	qgridLayout->addWidget(m_qspinbox_Width, index, 1);
	index++;

	p = new QLabel();
	p->setText("Height");
	p->setFixedSize(control_size);
	qgridLayout->addWidget(p, index, 0);
	m_qspinbox_Height = new QSpinBox();
	m_qspinbox_Height->setFixedSize(control_size);
	m_qspinbox_Height->setMinimum(0);
	m_qspinbox_Height->setMaximum(100000);
	qgridLayout->addWidget(m_qspinbox_Height, index, 1);
	index++;
	if (m_camera_index == 2 || m_camera_index == 3 || m_camera_index == 4)
	{
		p = new QLabel();
		p->setText("OffsetX");
		p->setFixedSize(control_size);
		qgridLayout->addWidget(p, index, 0);
		m_qspinbox_OffsetX = new QSpinBox();
		m_qspinbox_OffsetX->setFixedSize(control_size);
		m_qspinbox_OffsetX->setMinimum(0);
		m_qspinbox_OffsetX->setMaximum(100000);
		qgridLayout->addWidget(m_qspinbox_OffsetX, index, 1);
		index++;

		p = new QLabel();
		p->setText("OffsetY");
		p->setFixedSize(control_size);
		qgridLayout->addWidget(p, index, 0);
		m_qspinbox_OffsetY = new QSpinBox();
		m_qspinbox_OffsetY->setFixedSize(control_size);
		m_qspinbox_OffsetY->setMinimum(0);
		m_qspinbox_OffsetY->setMaximum(100000);
		qgridLayout->addWidget(m_qspinbox_OffsetY, index, 1);
		index++;

        p = new QLabel();
        p->setText("GainAuto Off");
        p->setFixedSize(control_size);
        qgridLayout->addWidget(p, index, 0);
        index++;

        p = new QLabel();
        p->setText("TriggerSource Software");
        p->setFixedSize(control_size);
        qgridLayout->addWidget(p, index, 0);
        index++;

        p = new QLabel();
        p->setText("ExposureAuto Off");
        p->setFixedSize(control_size);
        qgridLayout->addWidget(p, index, 0);
        index++;
	}
	if (m_camera_index == 0 || m_camera_index == 1)
	{
		p = new QLabel();
		p->setText("Acquisition Line Rate(in Hz)");
		p->setFixedSize(control_size);
		qgridLayout->addWidget(p, index, 0);
		m_doubleqspinbox_AcquisitionLineRate = new QDoubleSpinBox();
		m_doubleqspinbox_AcquisitionLineRate->setFixedSize(control_size);
		m_doubleqspinbox_AcquisitionLineRate->setMinimum(0);
		m_doubleqspinbox_AcquisitionLineRate->setMaximum(100000);
		qgridLayout->addWidget(m_doubleqspinbox_AcquisitionLineRate, index, 1);
		index++;

        p = new QLabel();
        p->setText("TriggerSource Line3");
        p->setFixedSize(control_size);
        qgridLayout->addWidget(p, index, 0);
        index++;
	}
	

    m_qpushbutton_save = new QPushButton();
    m_qpushbutton_save->setFixedSize(control_size);
    m_qpushbutton_save->setText("save");
	qgridLayout->addWidget(m_qpushbutton_save,index,0);
    connect(m_qpushbutton_save, &QPushButton::clicked, this, &CameraBaseQDialog::saveParameter);

    m_qpushbutton_hide = new QPushButton();
    m_qpushbutton_hide->setFixedSize(control_size);
    m_qpushbutton_hide->setText("exit");
	qgridLayout->addWidget(m_qpushbutton_hide,index,1);
    index++;


    loadParameter();



}

bool CameraBaseQDialog::initCamera(QString name,CameraCallback pfunction, void *pcontext)
{
    
    m_camera_interface = new CameraDalsa();

//    QMessageBox::critical(nullptr, m_camera_name,"initCamera");
    bool state = m_camera_interface->initCamera( name.toStdString().c_str(),pfunction, pcontext);


	if (state)
	{
        m_camera_interface->m_pAcqDevice->SetFeatureValue("TriggerSelector", "FrameStart");
        m_camera_interface->m_pAcqDevice->SetFeatureValue("TriggerMode", "On");
        if (m_camera_index == 0 || m_camera_index == 1 )
        {

//            m_camera_interface->m_pAcqDevice->SetFeatureValue("InternalScanDirection", "Forward");

//            m_camera_interface->m_pAcqDevice->SetFeatureValue("SensorTDIMode", "HighSensitivity");

            m_camera_interface->m_pAcqDevice->SetFeatureValue("TriggerSource", "Line3");

            m_camera_interface->m_pAcqDevice->SetFeatureValue("imageTimeoutEnable", false);
        }

        if (m_camera_index == 2 || m_camera_index == 3 || m_camera_index == 4)
        {

            m_camera_interface->m_pAcqDevice->SetFeatureValue("GainAuto", "Off");

            m_camera_interface->m_pAcqDevice->SetFeatureValue("TriggerSource", "Software");

            m_camera_interface->m_pAcqDevice->SetFeatureValue("ExposureAuto", "Off");
        }



		if (m_qspinbox_OffsetX != nullptr)
		{
//            QMessageBox::critical(nullptr, m_camera_name,"OffsetX");
			m_camera_interface->m_pAcqDevice->SetFeatureValue("OffsetX", OffsetX);
		}
		if (m_qspinbox_OffsetY != nullptr)
		{
//            QMessageBox::critical(nullptr, m_camera_name,"OffsetY");
			m_camera_interface->m_pAcqDevice->SetFeatureValue("OffsetY", OffsetY);
		}
		if (m_doubleqspinbox_AcquisitionLineRate != nullptr)
		{
//            QMessageBox::critical(nullptr, m_camera_name,"AcquisitionLineRate");;
			m_camera_interface->m_pAcqDevice->SetFeatureValue("AcquisitionLineRate", AcquisitionLineRate);
		}

//        QMessageBox::critical(nullptr, m_camera_name,"ExposureTime");;
		m_camera_interface->m_pAcqDevice->SetFeatureValue("ExposureTime", ExposureTime);
//        QMessageBox::critical(nullptr, m_camera_name,"Gain");;
		m_camera_interface->m_pAcqDevice->SetFeatureValue("Gain", Gain);
//        QMessageBox::critical(nullptr, m_camera_name,"Width");;
		m_camera_interface->m_pAcqDevice->SetFeatureValue("Width", Width);
//        QMessageBox::critical(nullptr, m_camera_name,"Height");;
		m_camera_interface->m_pAcqDevice->SetFeatureValue("Height", Height);
		
    }
	m_camera_interface->StartCapture();
    return state;
}

void CameraBaseQDialog::loadParameter()
{
	ExposureTime = m_qsettings->value(m_section + "/" + "ExposureTime", 0).toDouble();
	Gain = m_qsettings->value(m_section + "/" + "Gain", 0).toDouble();
	Width = m_qsettings->value(m_section + "/" + "Width", 100).toInt();
	Height = m_qsettings->value(m_section + "/" + "Height", 100).toInt();

	m_doubleqspinbox_ExposureTime->setValue(ExposureTime);
	m_doubleqspinbox_Gain->setValue(Gain);
	m_qspinbox_Width->setValue(Width);
	m_qspinbox_Height->setValue(Height);


	if (m_qspinbox_OffsetX != nullptr)
	{

		OffsetX = m_qsettings->value(m_section + "/" + "OffsetX", 0).toInt();
		m_qspinbox_OffsetX->setValue(OffsetX);

	}
	if (m_qspinbox_OffsetY != nullptr)
	{
		OffsetY = m_qsettings->value(m_section + "/" + "OffsetY", 0).toInt();
		m_qspinbox_OffsetY->setValue(OffsetY);

	}
	if (m_doubleqspinbox_AcquisitionLineRate != nullptr)
	{
		AcquisitionLineRate = m_qsettings->value(m_section + "/" + "AcquisitionLineRate", 0).toDouble();
		m_doubleqspinbox_AcquisitionLineRate->setValue(AcquisitionLineRate);

	}


}

//void CameraBaseQDialog::readCameraPara()
//{
//    if(m_camera_interface == nullptr)
//        return;
//    if(m_camera_interface->m_pAcqDevice == nullptr)
//        return;
//
//    if(m_camera_index==2 || m_camera_index==3||m_camera_index==4)
//    {
//
//       
//        
//
//        int index=0;
//        char name[256];
//        double double_value = 0.0;
//        int int_value = 0.0;
//        
//
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("ExposureTime", &double_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(double_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("Gain", &double_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(double_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("Width", &int_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(int_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("Height", &int_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(int_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("OffsetX", &int_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(int_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("OffsetY", &int_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(int_value)));
//		index++;
//
//    }
//    if(m_camera_index==0 || m_camera_index==1)
//    {
//        m_qtablewidget.setRowCount(5);
//        m_qtablewidget.setColumnCount(3);
//        int index=0;
//        char name[256];
//        double double_value = 0.0;
//        int int_value = 0.0;
//        BOOL bool_value;
//        
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("AcquisitionLineRate",&double_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(double_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("ExposureTime", &double_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(double_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("Gain", &double_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(double_value)));
//		index++;
//
//
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("Width", &int_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(int_value)));
//		index++;
//		
//		if (m_camera_interface->m_pAcqDevice->GetFeatureValue("Height", &int_value))
//		    m_qtablewidget.setItem(index, 2, new QTableWidgetItem(QString::number(int_value)));
//		index++;
//    }
//    for (int row = 0; row < m_qtablewidget.rowCount(); ++row) {
//        QTableWidgetItem *item = m_qtablewidget.item(row, 0); // 第一列索引为0
//        if (!item) {
//            item = new QTableWidgetItem(); // 确保单元格存在
//            m_qtablewidget.setItem(row, 0, item);
//        }
//        item->setBackground(QColor(200, 200, 200)); // 设置背景色
//    }
//    // 调整列宽自适应内容
//    // 关键设置：让表格水平扩展
//        m_qtablewidget.setEditTriggers(QAbstractItemView::NoEditTriggers);
//        m_qtablewidget.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
//
//        // 关键设置：让表头拉伸填充可用空间
//        m_qtablewidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
////    m_qtablewidget.horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
//    m_qtablewidget.show();
//}

void CameraBaseQDialog::saveParameter()
{
	
	ExposureTime = m_doubleqspinbox_ExposureTime->value();
	Gain = m_doubleqspinbox_Gain->value();
	Width = m_qspinbox_Width->value();
	Height = m_qspinbox_Height->value();
	/*if (m_camera_interface != nullptr)
	{
		m_camera_interface->m_pAcqDevice->SetFeatureValue("ExposureTime", ExposureTime);
		m_camera_interface->m_pAcqDevice->SetFeatureValue("Gain", Gain);
		m_camera_interface->m_pAcqDevice->SetFeatureValue("Width", Width);
		m_camera_interface->m_pAcqDevice->SetFeatureValue("Height", Height);
	}*/


	if (m_qspinbox_OffsetX != nullptr)
	{
		OffsetX = m_qspinbox_OffsetX->value();
		/*if (m_camera_interface != nullptr)
		{
			m_camera_interface->m_pAcqDevice->SetFeatureValue("OffsetX", OffsetX);
		}*/
	}
	if (m_qspinbox_OffsetY != nullptr)
	{
		OffsetY = m_qspinbox_OffsetY->value();
		/*if (m_camera_interface != nullptr)
		{
			m_camera_interface->m_pAcqDevice->SetFeatureValue("OffsetY", OffsetY);
		}*/
	}
	if (m_doubleqspinbox_AcquisitionLineRate != nullptr)
	{
		AcquisitionLineRate = m_doubleqspinbox_AcquisitionLineRate->value();
		/*if (m_camera_interface != nullptr)
		{
			m_camera_interface->m_pAcqDevice->SetFeatureValue("AcquisitionLineRate", AcquisitionLineRate);
		}*/
	}
	
	if (my_para.m_login_privilege >= 2)
	{
		m_qsettings->setValue(m_section + "/" + "ExposureTime", ExposureTime);
		m_qsettings->setValue(m_section + "/" + "Gain", Gain);
		m_qsettings->setValue(m_section + "/" + "Width", Width);
		m_qsettings->setValue(m_section + "/" + "Height", Height);

		if (m_qspinbox_OffsetX != nullptr)
		{
			
			m_qsettings->setValue(m_section + "/" + "OffsetX", OffsetX);
		}
		if (m_qspinbox_OffsetY != nullptr)
		{
			
			m_qsettings->setValue(m_section + "/" + "OffsetY", OffsetY);
		}
		if (m_doubleqspinbox_AcquisitionLineRate != nullptr)
		{
			
			m_qsettings->setValue(m_section + "/" + "AcquisitionLineRate", AcquisitionLineRate);
		}

	}
}

void CameraBaseQDialog::showImage(Mat image,int camera_index,int index)
{


//    QString message = m_type + " " + m_camera_name + "  "  + QString::number(index);
//    double ratio_x =  double(m_showqlable->width()) /  double(image.cols) ;
//    double ratio_y =  double(m_showqlable->height()) /  double(image.rows) ;
//    double ratio_image = ratio_x < ratio_y ?  ratio_x : ratio_y;

//    cv::resize(image, image, Size(image.cols * ratio_image , image.rows * ratio_image));

//    QImage q_image = matToQImage(image);
//    if (!q_image.isNull())
//    {
//        m_showqlable->loadtextimage(q_image,message,QColor(255,0,0));
//    }

}

void CameraBaseQDialog::softTrigger()
{

    if(m_camera_interface == nullptr)
    {
        logger->info("软触发失败");
//        QMessageBox::critical(nullptr, m_section, "相机初始化失败");
        return;
    }
    bool state = m_camera_interface->softTrigger();
    if(state==false)
        logger->info("软触发失败");
//        QMessageBox::critical(nullptr, m_camera_name,"软触发失败");;

}

void CameraBaseQDialog::closeEvent(QCloseEvent *event)
{
    if(m_camera_interface != nullptr)
    {
        m_camera_interface->unInitCamera();
    };
}
